package com.tydic.deviceParse;

import com.tydic.util.ByteUtils;

/**
 * Created by maliang on 2017/5/4.
 */
public class LoadometerParser {
    /**
     * 解析上海耀华的DS3地磅数据
     * 重量数据的格式为：第2~8个字节为重量整数部分
     * 第9个字节为小数部分：二进制从右到左后4个字符，有疑问？
     * @param data
     * @param comConfig
     * @return
     */
    public static String parseDs3(byte[] data,COMConfig comConfig){
        StringBuffer weight = new StringBuffer();
       try{
           String weightIntPart = ByteUtils.asciiToString(data,comConfig.getWeightIntStart(),comConfig.getWeightIntEnd());
           //截取后4位
    /*       String weightDecimalBinSrc = ByteUtils.hexString2binaryString(String.valueOf(data[8]));
           String weightDecimalBin = weightDecimalBinSrc.substring(weightDecimalBinSrc.length()-4,weightDecimalBinSrc.length());
           String weightDecimalPart = new BigInteger(weightDecimalBin,2).toString();
           weight.append(weightIntPart).append(".").append(weightDecimalPart);*/
           weight.append(Float.valueOf(weightIntPart));
       }catch (Exception e){
           //代表当前数据格式不满足地磅厂家协议，忽略
       }
        return weight.toString();
    }
}
